package hr.ctg.domain.model;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;
import org.jboss.seam.annotations.Name;

@Entity
@Name("dogadjaj")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name = "dogadjaj")
@NamedQueries( {
		@NamedQuery(name = "Dogadjaj.findAll", query = "select d from Dogadjaj d order by d.datumOd desc"),
		@NamedQuery(name = "Dogadjaj.findByCurrentDate", query = "select distinct d from Dogadjaj d join fetch d.opisDogadjajas o where d.datumOd >= CURRENT_DATE  order by d.datumOd asc"),
		@NamedQuery(name = "Dogadjaj.findByIdKorisnika", query = "select d from Dogadjaj d where d.korisnik.idKorisnika = ? order by d.datumOd desc"),
		@NamedQuery(name = "Dogadjaj.findByIdMjesta", query = "select d from Dogadjaj d where d.mjesto.idMjesta = ? and d.datumOd >= CURRENT_DATE order by d.datumOd asc"),
		@NamedQuery(name = "Dogadjaj.findByIdZupanije", query = "select d from Dogadjaj d where d.mjesto.zupanija.idZupanje = ? and d.datumOd >= CURRENT_DATE order by d.datumOd asc"),
		@NamedQuery(name = "Dogadjaj.findCountByIdZupanije", query = "select count(d) from Dogadjaj d where d.mjesto.zupanija.idZupanje = ?"),
		@NamedQuery(name = "Dogadjaj.findByIdOtoka", query = "select d from Dogadjaj d where d.mjesto.otok.idOtoka = ? and d.datumOd >= CURRENT_DATE order by d.datumOd asc"),
		@NamedQuery(name = "Dogadjaj.findCountByIdOtoka", query = "select count(d) from Dogadjaj d where d.mjesto.otok.idOtoka = ?"),
		@NamedQuery(name = "Dogadjaj.findCountByIdMjesta", query = "select count(d) from Dogadjaj d where d.mjesto.idMjesta = ?") })
public class Dogadjaj implements Serializable {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id_dogadjaja")
	private Integer idDogadjaja;

	@Column(name = "datum_od")
	private Date datumOd;

	@Column(name = "datum_do")
	private Date datumDo;

	private boolean trajanje;
	@Length(max = 100)
	private String www;
	@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "id_korisnika", nullable = false)
	@NotNull
	private Korisnik korisnik;

	@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
	@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
	@JoinColumn(name = "id_mjesta", unique = false, nullable = true, insertable = true, updatable = true)
	private Mjesto mjesto;

	@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "dogadjaj")
	private Set<Opis> opisDogadjajas = new HashSet<Opis>(0);

	@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "dogadjaj")
	private Set<Slika> slikas = new HashSet<Slika>(0);

	private static final long serialVersionUID = 1L;

	public Dogadjaj() {
		super();
	}

	public Integer getIdDogadjaja() {
		return idDogadjaja;
	}

	public void setIdDogadjaja(final Integer idDogadjaja) {
		this.idDogadjaja = idDogadjaja;
	}

	public Date getDatumOd() {
		return datumOd;
	}

	public void setDatumOd(final Date datumOd) {
		this.datumOd = datumOd;
	}

	public Date getDatumDo() {
		return datumDo;
	}

	public void setDatumDo(final Date datumDo) {
		this.datumDo = datumDo;
	}

	public Mjesto getMjesto() {
		return mjesto;
	}

	public void setMjesto(final Mjesto mjesto) {
		this.mjesto = mjesto;
	}

	public Set<Opis> getOpisDogadjajas() {
		return opisDogadjajas;
	}

	public void setOpisDogadjajas(final Set<Opis> opisDogadjajas) {
		this.opisDogadjajas = opisDogadjajas;
	}

	public boolean isTrajanje() {
		return trajanje;
	}

	public void setTrajanje(final boolean trajanje) {
		this.trajanje = trajanje;
	}

	public String getWww() {
		return www;
	}

	public void setWww(final String www) {
		this.www = www;
	}

	public Korisnik getKorisnik() {
		return korisnik;
	}

	public void setKorisnik(final Korisnik korisnik) {
		this.korisnik = korisnik;
	}

	public Opis getOpis(final String currentLanguage) {
		if (opisDogadjajas != null) {
			for (final Opis opis : opisDogadjajas) {
				if (opis.getJezik().getOznaka().equals(currentLanguage)) {
					return opis;
				}
			}
		}
		return null;
	}

	public Set<Slika> getSlikas() {
		return slikas;
	}

	public void setSlikas(final Set<Slika> slikas) {
		this.slikas = slikas;
	}
}
